'use strict';

/* Controllers */
app.controller('userCtrl', ['$scope', '$http', '$state', 'apiService', '$cookieStore', '$timeout', '$rootScope', 'toaster', function ($scope, $http, $state, apiService, $cookieStore, $timeout, $rootScope, toaster) {
    $rootScope.userObj = $cookieStore.get('cy__chongzhi');
    $scope.userInfo = {
        id: $rootScope.userObj['id'],
        username: $rootScope.userObj['username']
    }

    // 更新登录密码
    $scope.updatePass = function () {
        $scope.btn_disabled = true;
        toaster.pop('wait', '提示', '正在提交中...');
        apiService.post({
            modelName: 'account',
            apiName: 'updateAccountPwd'
        }, $scope.userInfo, function (data) {
            $scope.btn_disabled = false;
            if (data.code == 200) {
                toaster.clear();
                toaster.pop('success', '提示', data['msc']);
                $scope.userInfo = {
                    id: $rootScope.userObj['id'],
                    username: $rootScope.userObj['username']
                }
            } else {
                toaster.pop('error', '提示', data['msc']);
            }
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });
    }

    // 更新支付密码
    $scope.updatePayPass = function () {
        $scope.btn_disabled = true;
        toaster.pop('wait', '提示', '正在提交中...');
        apiService.post({
            modelName: 'account',
            apiName: 'updateAccountPayPwd'
        }, $scope.userInfo, function (data) {
            $scope.btn_disabled = false;
            if (data.code == 200) {
                toaster.clear();
                toaster.pop('success', '提示', data['msc']);
                $scope.userInfo = {
                    id: $rootScope.userObj['id'],
                    username: $rootScope.userObj['username']
                }
            } else {
                toaster.pop('error', '提示', data['msc']);
            }
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });
    }


    // 获取会员信息
    apiService.post({
        modelName: 'account',
        apiName: 'getAccountDB'
    }, $scope.userInfo, function (data) {
        if (data.code == 200) {
            $scope.userInfo = data['data'];
        } else {
            toaster.pop('error', '提示', data['msc']);
        }
    }, function (err) {
        toaster.pop('error', '提示', '服务器出小差咯...');
    });

    // 修改
    $scope.editInfo = function () {
        $scope.btn_disabled = true;
        // 更新会员信息
        apiService.post({
            modelName: 'account',
            apiName: 'updateAccountDB'
        }, $scope.userInfo, function (data) {
            $scope.btn_disabled = false;
            if (data.code == 200) {
                toaster.pop('success', '提示', data['msc']);
            } else {
                toaster.pop('error', '提示', data['msc']);
            }
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });
    };


}]);